pacman::p_load(foreign, dplyr)


# 1.Load data -------------------------------------------------------------

data_IndependentDirector <- read.dta(file = "Zheng_2017_CIE/material/数据（独董层面）.dta")
data_Company <- read.dta(file = "Zheng_2017_CIE/material/数据（公司层面）.dta")


# 2.Descriptive statistics ------------------------------------------------

salary <- data.frame(
  year = data_IndependentDirector$year,
  Pay = exp(1) ^ data_IndependentDirector$lnPay
)
## Table 1
for (Year in unique(salary$year)) {
  number = nrow(salary[salary$year == Year, ])
  mean = mean(salary[salary$year == Year, "Pay"], na.rm = T) %>% 
    round(2)
  ratio = round(number / nrow(data_IndependentDirector) * 100, 2)
  print(paste0(Year, "年 ", number, " ", ratio, "% ", mean))
  remove(Year, number, ratio, mean)
}
## Table 2
salary_company <- data_IndependentDirector[, c(1, 2, 4)]
salary_company[!duplicated(salary_company), ] %>% 
  count(code, year) %>% 
  mutate(n = ifelse(n > 1, 2, 1)) %>% 
  group_by(year) %>% 
  summarise(chayi = sum(n) - n(),
            chayi_ratio = chayi / n())
## Table 3
str(data_Company[, 7:22])

